package com.wps.shell.controls
{
	import com.wps.shell.events.WindowEvent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.containers.ViewStack;
	import mx.effects.Blur;
	import mx.effects.Fade;
	import mx.effects.Move;
	import mx.effects.Parallel;
	import mx.events.FlexEvent;
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;

	/**
	 * 对话框关闭事件。
	 * @eventType mx.events.CloseEvent.CLOSE
	 */
	[Event(name="close", type="mx.events.CloseEvent")]

	/**
	 * 通用对话框
	 * @author zhuohongtao
	 */
	public class CommonDialog extends CommonWindow
	{
		//--------------------------------------------------------------------------
		//
		//  常量定义，用于对话框的返回值。
		//
		//--------------------------------------------------------------------------

		public static const OK: int			= 0;
		public static const CANCEL: int		= 1;
		public static const ERROR: int		= 2;
		public static const YES: int		= 3;
		public static const NO: int			= 4;

		//--------------------------------------------------------------------------
		//
		//  私有变量
		//
		//--------------------------------------------------------------------------

		private var _closefunction: Function = null;
		private var _minWidth: uint = 0;
		private var _minHeight: uint = 0;
		private var _autoDestroy: Boolean = false;
		private var _pageContainers: Vector.<ViewStack> = new Vector.<ViewStack>();
		private var _showEffect: Parallel = null;
		private var _moveInEffect: Move = null;

		private var _willBeDestroy: Boolean; //标记窗口是否被害destroy bug70787
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		/**
		 * 构造函数。
		 */
		public function CommonDialog()
		{
			super();
			isPopUp = false;
			minimizable = false;
			maximizable = false;
//			resizeable = false;
			_willBeDestroy = false;
			addEventListener(FlexEvent.CREATION_COMPLETE, _createCompleteHandler);
			addEventListener(FlexEvent.CONTENT_CREATION_COMPLETE, _contentCreateCompleteHandler);
		}

		//--------------------------------------------------------------------------
		//
		//  ILifecycle
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 */
		override public function destroy(): void
		{
			_willBeDestroy = true;

			before_destroy();

			_pageContainers = null;
			_showEffect = null;
			_moveInEffect = null;
			super.destroy();
		}

		//--------------------------------------------------------------------------
		//
		//  公开属性
		//
		//--------------------------------------------------------------------------

		/**
		 * 客户区宽度。
		 */
		[Inspectable(category="Size", defaultValue="0")]
		public function get clientWidth(): uint
		{
			var leftValue: Number = getStyle("borderThicknessLeft");
			if(isNaN(leftValue))
				leftValue = CommonWindow.BORDERWIDTH;
			var rightValue: Number = getStyle("borderThicknessRight");
			if(isNaN(rightValue))
				rightValue = CommonWindow.BORDERWIDTH;
			return width - leftValue - rightValue;
		}

		public function set clientWidth(value: uint): void
		{
			var leftValue: Number = getStyle("borderThicknessLeft");
			if(isNaN(leftValue))
				leftValue = CommonWindow.BORDERWIDTH;
			var rightValue: Number = getStyle("borderThicknessRight");
			if(isNaN(rightValue))
				rightValue = CommonWindow.BORDERWIDTH;
			width = value + leftValue + rightValue;
		}

		/**
		 * 客户区高度。
		 */
		[Inspectable(category="Size", defaultValue="0")]
		public function get clientHeight(): uint
		{
			var topValue: Number = getStyle("headerHeight");
			if(isNaN(topValue))
				topValue = CommonWindow.MIN_HEIGHT;
			var bottomValue: Number = getStyle("borderThicknessBottom");
			if(isNaN(bottomValue))
				bottomValue = CommonWindow.BORDERWIDTH;
			return height - topValue - bottomValue;
		}

		public function set clientHeight(value: uint): void
		{
			var topValue: Number = getStyle("headerHeight");
			if(isNaN(topValue))
				topValue = CommonWindow.MIN_HEIGHT;
			var bottomValue: Number = getStyle("borderThicknessBottom");
			if(isNaN(bottomValue))
				bottomValue = CommonWindow.BORDERWIDTH;
			height = value + topValue + bottomValue;
		}

		/**
		 * @private
		 * minWidth属性值由_minClientWidth得到。
		 */
		override public function get minWidth(): Number
		{
			if (_minWidth != 0)
				return _minWidth;
			else
				return super.minWidth;
		}

		/**
		 * @private
		 * minHeight属性值由_minClientHeight得到。
		 */
		override public function get minHeight(): Number
		{
			if (_minHeight != 0)
				return _minHeight;
			else
				return super.minHeight;
		}

		/**
		 * 屏蔽对话框的width属性，使用clientWidth属性。
		 */
		override public function get width(): Number
		{
			if (contentGroup && (super.width == 0))
			{
				return contentGroup.width + contentGroup.left + contentGroup.right;
			}
			return super.width;
		}

		/**
		 * 屏蔽对话框的height属性，使用clientHeight属性。
		 */
		override public function get height(): Number
		{
			if (contentGroup && (super.height == 0))
			{
				return contentGroup.height + contentGroup.top  + contentGroup.bottom;
			}
			return super.height;
		}

		//--------------------------------------------------------------------------
		//
		//  公开方法
		//
		//--------------------------------------------------------------------------

		/**
		 * 显示模态对话框。
		 * @param parent 对话框在其上居中的对象。
		 * @param closefunction 对话框关闭时的事件处理函数。
		 * @param autoDestroy 对话框关闭时自动析构。
		 * @param showEffect 是否显示动画效果
		 */
		final public function showModal(parent: DisplayObject, closefunction: Function = null, autoDestroy: Boolean = true, showEffect: Boolean = true): void
		{
			_autoDestroy = autoDestroy;
			_showDialog(parent, true, closefunction, showEffect);
		}

		/**
		 * 关闭模态对话框。
		 * @param code 对话框的返回值，该值会返回给用户定义的事件处理函数。
		 */
		final public function endModal(code: int): void
		{
			_closeDialog(code);
			if (_autoDestroy)
				destroy();
		}

		/**
		 * 显示非模态对话框。
		 * @param parent 对话框在其上居中的对象。
		 * @param showEffect 是否显示动画效果
		 */
		final public function show(parent: DisplayObject, showEffect: Boolean = true): void
		{
			_showDialog(parent, false, null, showEffect);
		}

		/**
		 * 关闭非模态对话框。
		 */
		final public function close(): void
		{
			_closeDialog(OK);
		}

		//--------------------------------------------------------------------------
		//
		//  重载方法
		//
		//--------------------------------------------------------------------------
		/**
		 * 语言环境发生变化时，更新界面字符串。
		 * @private
		 */
		override protected function resourcesChanged(): void
		{
			super.resourcesChanged();
			if (initialized)
				update_resources();
		}

		/**
		 * 提交属性时，更新快捷键列表。
		 * @private
		 */
		override protected function commitProperties(): void
		{
			super.commitProperties();
			if (initialized)
			{
				validateNow();
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  需由派生类重写的方法
		//
		//--------------------------------------------------------------------------

		/**
		 * 初始化界面，对话框创建完成时调用。
		 */
		protected function initialize_interface(): void
		{
		}

		/**
		 * 更新资源字符串，当语言环境发送变化时调用。
		 */
		protected function update_resources(): void
		{
		}

		/**
		 * 清理资源，对话框关闭之前调用。
		 */
		protected function before_close(): void
		{
		}

		/**
		 * 清理资源，对话框销毁时调用。
		 */
		protected function before_destroy(): void
		{
		}

		/**
		 * 有些操作可以在内容组件创建后进行，先于initialize_interface执行
		 */
		protected function onContentCreateComplete(): void
		{

		}

		//--------------------------------------------------------------------------
		//
		//  事件处理器
		//
		//--------------------------------------------------------------------------

		/**
		 * 对话框创建完成的处理函数
		 * @private
		 */
		private function _createCompleteHandler(event: FlexEvent): void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, _createCompleteHandler);

			_minWidth = this.width;
			_minHeight = this.height;

			update_resources();
			initialize_interface();
			_initEffects();
		}

		/**
		 *  内容组件创建完成的处理函数
		 */
		private function _contentCreateCompleteHandler(event: FlexEvent): void
		{
			removeEventListener(FlexEvent.CONTENT_CREATION_COMPLETE, _contentCreateCompleteHandler);
			onContentCreateComplete();
		}

		/**
		 * 窗口状态发生变化的事件处理器，解决对话框最大化时，对话框宽高跟随舞台大小变化的问题。
		 * @private
		 */
		private function _windowStateChangeHandler(event: WindowEvent): void
		{
			switch (event.type)
			{
				case WindowEvent.WINDOW_MAXIMIZE:
					width = parent.width;
					height = parent.height;
					systemManager.addEventListener(ResizeEvent.RESIZE, _stageResizeHandler);
					break;
				case WindowEvent.WINDOW_MINIMIZE:
				case WindowEvent.WINDOW_RESTORE:
					systemManager.removeEventListener(ResizeEvent.RESIZE, _stageResizeHandler);
					break;
			}
		}

		/**
		 * 处于最大化状态的对话框，在舞台大小发生变化时更新对话框的大小。
		 * @private
		 */
		private function _stageResizeHandler(event: Event): void
		{
			if (windowState == CommonWindow.WINDOW_STATE_MAXIMIZED)
			{
				var p: DisplayObject = parent != null ? parent : systemManager.stage;
				width = p.width;
				height = p.height;
			}
		}


		//--------------------------------------------------------------------------
		//
		//  辅助方法
		//
		//--------------------------------------------------------------------------

		/**
		 * 显示对话框
		 * @param parent 对话框在其上居中的对象
		 * @param modal 是否显示为模态对话框
		 * @param closefunction 对话框关闭时的事件处理函数
		 * @param showEffect 是否显示动画效果
		 */
		private function _showDialog(parent: DisplayObject, modal: Boolean, closefunction: Function = null, showEffect: Boolean = true): void
		{
			addEventListener(WindowEvent.WINDOW_MAXIMIZE, _windowStateChangeHandler);
			addEventListener(WindowEvent.WINDOW_MINIMIZE, _windowStateChangeHandler);
			addEventListener(WindowEvent.WINDOW_RESTORE, _windowStateChangeHandler);

			_closefunction = closefunction;
			PopUpManager.addPopUp(this, parent, modal);
			PopUpManager.centerPopUp(this);
			setFocus();

			if (showEffect)
			{
				_setupEffects();
				if (_showEffect)
					_showEffect.play();
			}
		}

		/**
		 * 关闭对话框
		 * @param code 对话框的返回值，该值会返回给用户定义的事件处理函数。
		 */
		private function _closeDialog(code: int): void
		{
			before_close();
			if (_closefunction != null)
			{
				try
				{
					_closefunction(this, code);
				}
				finally
				{
					_closefunction = null;
				}
			}

			_clearPageContainers();

			removeEventListener(WindowEvent.WINDOW_MAXIMIZE, _windowStateChangeHandler);
			removeEventListener(WindowEvent.WINDOW_MINIMIZE, _windowStateChangeHandler);
			removeEventListener(WindowEvent.WINDOW_RESTORE, _windowStateChangeHandler);

			PopUpManager.removePopUp(this);
		}

		/**
		 * 清除多页控件的事件处理器
		 * @private
		 */
		private function _clearPageContainers(): void
		{
			if (!_pageContainers) return;

			for each (var container: ViewStack in _pageContainers)
			_pageContainers.length = 0;
		}

		/**
		 * 初始化窗体特效
		 */
		private function _initEffects():void
		{
			_showEffect = new Parallel(this);
			_showEffect.duration = 150;

			_moveInEffect = new Move();

			var _blurIn: Blur = new Blur();
			_blurIn.blurXFrom = 16;
			_blurIn.blurYFrom = 16;
			_blurIn.blurXTo = 0;
			_blurIn.blurYTo = 0;

			var _fadeIn: Fade = new Fade();
			_fadeIn.alphaFrom = 0.3;
			_fadeIn.alphaTo = 1;

			_showEffect.addChild(_moveInEffect);
			_showEffect.addChild(_blurIn);
			_showEffect.addChild(_fadeIn);
		}

		/**
		 * 设置窗体特效
		 */
		private function _setupEffects():void
		{
			if (_moveInEffect)
			{
				_moveInEffect.xFrom = x - 10;
				_moveInEffect.xTo = x;
			}
		}
	}
}
